//////////////////////////////////////////
// BIOD BODY CLASS						//
// 鸟的可视化
//////////////////////////////////////////
class BiodBody {
    constructor(svg, biod, isPredator) {
        this.svg = svg;
        this.bipod = biod;
        this.isPredator = isPredator;

        this.svgBody = document.createElementNS(
            "http://www.w3.org/2000/svg",
            "circle"
        );
        this.svgBody.setAttribute("cx", "0");
        this.svgBody.setAttribute("cy", "0");
        this.svgBody.setAttribute("r", this.bipod.radius - 2); //-2 for border.
        this.svgBody.style.fill = "none";
        this.svgBody.style.stroke = "#000";
        this.svgBody.style.strokeWidth = "2px";

        this.svgHeading = document.createElementNS(
            "http://www.w3.org/2000/svg",
            "line"
        );
        this.svgHeading.setAttribute("x1", "0");
        this.svgHeading.setAttribute("y1", "0");
        this.svgHeading.setAttribute("x2", "10");
        this.svgHeading.setAttribute("y2", "10");
        this.svgHeading.style.stroke = "black";
        this.svgHeading.style.strokeWidth = "1px";

        //Add to document.
        this.svg.appendChild(this.svgBody);
        this.svg.appendChild(this.svgHeading);
    }

    die() {
        this.svg.removeChild(this.svgBody);
        this.svg.removeChild(this.svgHeading);
    }

    update() {
        this.svgBody.setAttribute("cx", this.bipod.position.x);
        this.svgBody.setAttribute("cy", this.bipod.position.y);

        this.svgHeading.setAttribute("x1", this.bipod.position.x);
        this.svgHeading.setAttribute("y1", this.bipod.position.y);

        var dir = this.bipod.position.add(
            this.bipod.heading.scale(this.bipod.radius * 2 - 4)
        );
        this.svgHeading.setAttribute("x2", dir.x);
        this.svgHeading.setAttribute("y2", dir.y);

        //Cohesion = green.
        //Claustrophobia = red.
        //Emitter = blue.
        //Predator = purple.
        if (this.isPredator) {
            this.svgBody.style.fill = "#BE81F7";
        } else {
            if (this.bipod.majorInfluence == "cohesion") {
                this.svgBody.style.fill = "#A5DF00";
            } else {
                if (this.bipod.majorInfluence == "claustrophobia")
                    this.svgBody.style.fill = "#FE642E";
                else this.svgBody.style.fill = "#58D3F7";
            }
        }
    }
}

export { BiodBody };
